<?php
namespace app\admin\controller;

use think\Controller;
use think\facade\Cache;
use app\common\lib\Dir;

class Common extends Controller
{
    protected $config;
	protected $soo_broker;
    
    //构造函数
    public function __construct()
    {
		$this->soo_broker = new \Jasny\SSO\Broker(config('app.sso.sso_server'), config('app.sso.sso_broker_id'), config('app.sso.sso_broker_secret'));
        $this->sso_login();
        $data = [
            [
                'id'=>1,
                'value' =>3600
            ]
        ];
        Cache::set('config_set', $data, 0);
        parent::__construct();
    }
	
	//初始化
	protected function initialize()
	{
	    $this->config = db('config')->cache('config', 0, 'config')->column('*', 'name');
		
		
	    $isLogin = $this->isLogin();
	    if (!$isLogin) {
	        return $this->redirect('login/index');
	    }
	}
	//单点登录验证
	public function sso_login(){
		
		$this->soo_broker->attach(true);
		
		$user = null;
		try {
		    $user = $this->soo_broker->getUserInfo();
		} catch (NotAttachedException $e) {
		    header('Location: ' . $_SERVER['REQUEST_URI']);
		    exit;
		} catch (SsoException $e) {
		    $this->error($e->getMessage(), '');
		}
		
		if($user){
			// 有用户信息 先取本地session,等到用户$u
			$u = session('admin');
			if($u){
				if($u['id'] != $user['id']){
					//替换新用户session
					session('admin',$user);
				}
			}else{
				//设置本地session,新的$user进行登录
				session('admin',$user);
			}
		}else{
			//没有登录 清除session,跳转登录页
			session('admin',null);
		}
	}
	
    //验证是否登录
    public function isLogin()
    {
        $user = session('admin');
        if ($user['id']) {
            $this->assign('user', $user);
            return true;
        }
        return false;
    }
    //语言切换
    public function lang()
    {
        $data = request()->param();
        switch ($data['type']) {
            case 'zh':
                cookie('think_var', 'zh-cn');
                break;
            case 'en':
                cookie('think_var', 'en-us');
                break;
            default:
                cookie('think_var', 'zh-cn');
        }
        return show(1, "设置成功", 0, 200);
    }
    //清除缓存
    public function clearCache()
    {
        $data = request()->param();
        switch ($data['type']) {
            case '1':
                $data = Cache::clear();
                $data = Dir::del('runtime/temp');
                $data = Dir::del('runtime/log');
                break;
            case '2':
                $data = Dir::del('runtime/temp');
                break;
            case '3':
                $data = Cache::clear();
                break;
            default:
                $data = Cache::clear();
        }
        
        
        if ($data) {
            return show(1, lang("cache_clear_success"), 0, 200);
        } else {
            return show(0, lang("cache_clear_error"), 0, 200);
        }
    }
	
	public function outLogin(){
		$this->soo_broker->logout();
		session('admin',null);
		return show(0,'退出成功',[],200);
	}
}
